import StringTool from "../../../common/tool/StringTool";
import CountDownControl from "./CountDownControl";

export enum CountDownShowType {
    /**
     * 完整
     */
    All = 1,

    /**
     * 小时
     */
    Hour,

    /**
     * 
     */
    AllHour,

    /**
     * 天
     */
    Day,


    OnlyDay,

    /**
     * 分钟
     */
    Minute,

    /**
     * 秒
     */
    Second,
}

const { ccclass, property } = cc._decorator;
@ccclass
export default class CountDownLabel extends CountDownControl {

    @property(cc.Label)
    mLabel: cc.Label = null;
    /**
     * 倒计时类型
     */

    @property({ type: cc.Enum(CountDownShowType), visible: true })
    public mType: CountDownShowType = CountDownShowType.All;

    public refreshUI(): void {
        this.refereshLabelShow();
    }

    private refereshLabelShow(): void {

        let nowTime = this.mBookTime * CountDownControl.TIME_INTERVAL;
        switch (this.mType) {
            case CountDownShowType.All:
                this.showAllTime(nowTime);
                break;
            case CountDownShowType.Hour:
                this.showHourTime(nowTime);
                break;
            case CountDownShowType.AllHour:
                this.showAllHourTime(nowTime);
                break;
            case CountDownShowType.Day:
                this.showDayTime(nowTime);
                break;
            case CountDownShowType.OnlyDay:
                this.showOnlyDayTime(nowTime);
                break;
            case CountDownShowType.Minute:
                this.showMinnuteTime(nowTime);
                break;
            default:
                break;
        }
    }

    private showAllTime(nowTime: number): void {
        let hours = parseInt(nowTime / (60 * 60) + "");
        let min = parseInt(nowTime % (60 * 60) / 60 + "");
        let sec = nowTime % (60 * 60) % 60 % 60;

        let ht = StringTool.RepairTenNumber(hours);
        let mt = StringTool.RepairTenNumber(min);
        let st = StringTool.RepairTenNumber(sec);

        this.mLabel.string = ht + ":" + mt + ":" + st;
    }

    private showHourTime(nowTime: number): void {
        let min = parseInt(nowTime % (60 * 60) / 60 + "");
        let sec = nowTime % (60 * 60) % 60 % 60;
        let mt = StringTool.RepairTenNumber(min);
        let st = StringTool.RepairTenNumber(sec);
        this.mLabel.string = mt + ":" + st;
    }

    private showAllHourTime(nowTime: number): void {
        let min = parseInt(nowTime % (60 * 60) / 60 + "");
        let sec = nowTime % (60 * 60) % 60 % 60;

        let mt = StringTool.RepairTenNumber(min);
        let st = StringTool.RepairTenNumber(sec);
        this.mLabel.string = mt + ":" + st;
    }

    private showDayTime(nowTime: number): void {
        let day = parseInt(nowTime / (60 * 60 * 24) + "");
        let leaveTime = parseInt(nowTime % (60 * 60 * 24) + "");
        let hours = parseInt(leaveTime / (60 * 60) + "") + (day * 24);
        let min = parseInt(leaveTime % (60 * 60) / 60 + "");
        let sec = parseInt(leaveTime % (60 * 60) % 60 % 60 + "");
        this.mLabel.string = "Time left: "
            + StringTool.RepairTenNumber(hours) +
            ":" + StringTool.RepairTenNumber(min) +
            ":" + StringTool.RepairTenNumber(sec);
    }

    private showOnlyDayTime(nowTime: number): void {
        let day = parseInt(nowTime / (60 * 60 * 24) + "");
        let leaveTime = parseInt(nowTime % (60 * 60 * 24) + "");
        let hours = parseInt(leaveTime / (60 * 60) + "") + (day * 24);
        let min = parseInt(leaveTime % (60 * 60) / 60 + "");
        let sec = parseInt(leaveTime % (60 * 60) % 60 % 60 + "");

        this.mLabel.string =
            StringTool.RepairTenNumber(hours)
            + ":" + StringTool.RepairTenNumber(min)
            + ":" + StringTool.RepairTenNumber(sec);
    }

    private showMinnuteTime(nowTime: number): void {
        let min = parseInt(nowTime / 60 + "");
        let sec = parseInt(nowTime % 60 + "");
        this.mLabel.string = StringTool.RepairTenNumber(min) + ":" + StringTool.RepairTenNumber(sec);
    }
}


